
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>动态路由匹配 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="nested-routes.html" />
    
    
    <link rel="prev" href="getting-started.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../installation.html">
            
                <a href="../installation.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    基础
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="getting-started.html">
            
                <a href="getting-started.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.2" data-path="dynamic-matching.html">
            
                <a href="dynamic-matching.html">
            
                    
                    动态路由匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="nested-routes.html">
            
                <a href="nested-routes.html">
            
                    
                    嵌套路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="navigation.html">
            
                <a href="navigation.html">
            
                    
                    编程式导航
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="named-routes.html">
            
                <a href="named-routes.html">
            
                    
                    命名路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="named-views.html">
            
                <a href="named-views.html">
            
                    
                    命名视图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="redirect-and-alias.html">
            
                <a href="redirect-and-alias.html">
            
                    
                    重定向 和 别名
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="history-mode.html">
            
                <a href="history-mode.html">
            
                    
                    HTML5 History 模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    进阶
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../advanced/navigation-guards.html">
            
                <a href="../advanced/navigation-guards.html">
            
                    
                    导航钩子
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../advanced/meta.html">
            
                <a href="../advanced/meta.html">
            
                    
                    路由元信息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../advanced/transitions.html">
            
                <a href="../advanced/transitions.html">
            
                    
                    过渡动效
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../advanced/data-fetching.html">
            
                <a href="../advanced/data-fetching.html">
            
                    
                    数据获取
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../advanced/scroll-behavior.html">
            
                <a href="../advanced/scroll-behavior.html">
            
                    
                    滚动行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../advanced/lazy-loading.html">
            
                <a href="../advanced/lazy-loading.html">
            
                    
                    懒加载
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    API 文档
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../api/router-link.html">
            
                <a href="../api/router-link.html">
            
                    
                    router-link
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../api/router-view.html">
            
                <a href="../api/router-view.html">
            
                    
                    router-view
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../api/route-object.html">
            
                <a href="../api/route-object.html">
            
                    
                    路由信息对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../api/options.html">
            
                <a href="../api/options.html">
            
                    
                    Router 构造配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../api/router-instance.html">
            
                <a href="../api/router-instance.html">
            
                    
                    Router 实例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../api/component-injections.html">
            
                <a href="../api/component-injections.html">
            
                    
                    对组件注入
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >动态路由匹配</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x52A8;&#x6001;&#x8DEF;&#x7531;&#x5339;&#x914D;">&#x52A8;&#x6001;&#x8DEF;&#x7531;&#x5339;&#x914D;</h1>
<p>&#x6211;&#x4EEC;&#x7ECF;&#x5E38;&#x9700;&#x8981;&#x628A;&#x67D0;&#x79CD;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x5230;&#x7684;&#x6240;&#x6709;&#x8DEF;&#x7531;&#xFF0C;&#x5168;&#x90FD;&#x6620;&#x5C04;&#x5230;&#x540C;&#x4E2A;&#x7EC4;&#x4EF6;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x6211;&#x4EEC;&#x6709;&#x4E00;&#x4E2A; <code>User</code> &#x7EC4;&#x4EF6;&#xFF0C;&#x5BF9;&#x4E8E;&#x6240;&#x6709; ID &#x5404;&#x4E0D;&#x76F8;&#x540C;&#x7684;&#x7528;&#x6237;&#xFF0C;&#x90FD;&#x8981;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x7EC4;&#x4EF6;&#x6765;&#x6E32;&#x67D3;&#x3002;&#x90A3;&#x4E48;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;  <code>vue-router</code> &#x7684;&#x8DEF;&#x7531;&#x8DEF;&#x5F84;&#x4E2D;&#x4F7F;&#x7528;&#x300E;&#x52A8;&#x6001;&#x8DEF;&#x5F84;&#x53C2;&#x6570;&#x300F;&#xFF08;dynamic segment&#xFF09;&#x6765;&#x8FBE;&#x5230;&#x8FD9;&#x4E2A;&#x6548;&#x679C;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> User = {
  template: <span class="hljs-string">&apos;&lt;div&gt;User&lt;/div&gt;&apos;</span>
}

<span class="hljs-keyword">const</span> router = <span class="hljs-keyword">new</span> VueRouter({
  routes: [
    <span class="hljs-comment">// &#x52A8;&#x6001;&#x8DEF;&#x5F84;&#x53C2;&#x6570; &#x4EE5;&#x5192;&#x53F7;&#x5F00;&#x5934;</span>
    { path: <span class="hljs-string">&apos;/user/:id&apos;</span>, component: User }
  ]
})
</code></pre>
<p>&#x73B0;&#x5728;&#x5462;&#xFF0C;&#x50CF; <code>/user/foo</code> &#x548C; <code>/user/bar</code> &#x90FD;&#x5C06;&#x6620;&#x5C04;&#x5230;&#x76F8;&#x540C;&#x7684;&#x8DEF;&#x7531;&#x3002;</p>
<p>&#x4E00;&#x4E2A;&#x300E;&#x8DEF;&#x5F84;&#x53C2;&#x6570;&#x300F;&#x4F7F;&#x7528;&#x5192;&#x53F7; <code>:</code> &#x6807;&#x8BB0;&#x3002;&#x5F53;&#x5339;&#x914D;&#x5230;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x65F6;&#xFF0C;&#x53C2;&#x6570;&#x503C;&#x4F1A;&#x88AB;&#x8BBE;&#x7F6E;&#x5230;
 <code>this.$route.params</code>&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x6BCF;&#x4E2A;&#x7EC4;&#x4EF6;&#x5185;&#x4F7F;&#x7528;&#x3002;&#x4E8E;&#x662F;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x66F4;&#x65B0; <code>User</code> &#x7684;&#x6A21;&#x677F;&#xFF0C;&#x8F93;&#x51FA;&#x5F53;&#x524D;&#x7528;&#x6237;&#x7684; ID&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> User = {
  template: <span class="hljs-string">&apos;&lt;div&gt;User {{ $route.params.id }}&lt;/div&gt;&apos;</span>
}
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x770B;&#x770B;&#x8FD9;&#x4E2A;<a href="http://jsfiddle.net/yyx990803/4xfa2f19/" target="_blank">&#x5728;&#x7EBF;&#x4F8B;&#x5B50;</a>.</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x4E2D;&#x8BBE;&#x7F6E;&#x591A;&#x6BB5;&#x300E;&#x8DEF;&#x5F84;&#x53C2;&#x6570;&#x300F;&#xFF0C;&#x5BF9;&#x5E94;&#x7684;&#x503C;&#x90FD;&#x4F1A;&#x8BBE;&#x7F6E;&#x5230; <code>$route.params</code> &#x4E2D;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>&#x6A21;&#x5F0F;</th>
<th>&#x5339;&#x914D;&#x8DEF;&#x5F84;</th>
<th>$route.params</th>
</tr>
</thead>
<tbody>
<tr>
<td>/user/:username</td>
<td>/user/evan</td>
<td><code>{ username: &apos;evan&apos; }</code></td>
</tr>
<tr>
<td>/user/:username/post/:post_id</td>
<td>/user/evan/post/123</td>
<td><code>{ username: &apos;evan&apos;, post_id: 123 }</code></td>
</tr>
</tbody>
</table>
<p>&#x9664;&#x4E86; <code>$route.params</code> &#x5916;&#xFF0C;<code>$route</code> &#x5BF9;&#x8C61;&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x5176;&#x5B83;&#x6709;&#x7528;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x4F8B;&#x5982;&#xFF0C;<code>$route.query</code>&#xFF08;&#x5982;&#x679C; URL &#x4E2D;&#x6709;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#xFF09;&#x3001;<code>$route.hash</code> &#x7B49;&#x7B49;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x67E5;&#x770B; <a href="../api/route-object.html">API &#x6587;&#x6863;</a> &#x7684;&#x8BE6;&#x7EC6;&#x8BF4;&#x660E;&#x3002;</p>
<h3 id="&#x54CD;&#x5E94;&#x8DEF;&#x7531;&#x53C2;&#x6570;&#x7684;&#x53D8;&#x5316;">&#x54CD;&#x5E94;&#x8DEF;&#x7531;&#x53C2;&#x6570;&#x7684;&#x53D8;&#x5316;</h3>
<p>&#x63D0;&#x9192;&#x4E00;&#x4E0B;&#xFF0C;&#x5F53;&#x4F7F;&#x7528;&#x8DEF;&#x7531;&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x4F8B;&#x5982;&#x4ECE; <code>/user/foo</code> &#x5BFC;&#x822A;&#x5230; <code>user/bar</code>&#xFF0C;<strong>&#x539F;&#x6765;&#x7684;&#x7EC4;&#x4EF6;&#x5B9E;&#x4F8B;&#x4F1A;&#x88AB;&#x590D;&#x7528;</strong>&#x3002;&#x56E0;&#x4E3A;&#x4E24;&#x4E2A;&#x8DEF;&#x7531;&#x90FD;&#x6E32;&#x67D3;&#x540C;&#x4E2A;&#x7EC4;&#x4EF6;&#xFF0C;&#x6BD4;&#x8D77;&#x9500;&#x6BC1;&#x518D;&#x521B;&#x5EFA;&#xFF0C;&#x590D;&#x7528;&#x5219;&#x663E;&#x5F97;&#x66F4;&#x52A0;&#x9AD8;&#x6548;&#x3002;<strong>&#x4E0D;&#x8FC7;&#xFF0C;&#x8FD9;&#x4E5F;&#x610F;&#x5473;&#x7740;&#x7EC4;&#x4EF6;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;&#x4E0D;&#x4F1A;&#x518D;&#x88AB;&#x8C03;&#x7528;</strong>&#x3002;</p>
<p>&#x590D;&#x7528;&#x7EC4;&#x4EF6;&#x65F6;&#xFF0C;&#x60F3;&#x5BF9;&#x8DEF;&#x7531;&#x53C2;&#x6570;&#x7684;&#x53D8;&#x5316;&#x4F5C;&#x51FA;&#x54CD;&#x5E94;&#x7684;&#x8BDD;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x7B80;&#x5355;&#x5730; watch&#xFF08;&#x76D1;&#x6D4B;&#x53D8;&#x5316;&#xFF09; <code>$route</code> &#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> User = {
  template: <span class="hljs-string">&apos;...&apos;</span>,
  watch: {
    <span class="hljs-string">&apos;$route&apos;</span> (to, <span class="hljs-keyword">from</span>) {
      <span class="hljs-comment">// &#x5BF9;&#x8DEF;&#x7531;&#x53D8;&#x5316;&#x4F5C;&#x51FA;&#x54CD;&#x5E94;...</span>
    }
  }
}
</code></pre>
<h3 id="&#x9AD8;&#x7EA7;&#x5339;&#x914D;&#x6A21;&#x5F0F;">&#x9AD8;&#x7EA7;&#x5339;&#x914D;&#x6A21;&#x5F0F;</h3>
<p><code>vue-router</code> &#x4F7F;&#x7528; <a href="https://github.com/pillarjs/path-to-regexp" target="_blank">path-to-regexp</a> &#x4F5C;&#x4E3A;&#x8DEF;&#x5F84;&#x5339;&#x914D;&#x5F15;&#x64CE;&#xFF0C;&#x6240;&#x4EE5;&#x652F;&#x6301;&#x5F88;&#x591A;&#x9AD8;&#x7EA7;&#x7684;&#x5339;&#x914D;&#x6A21;&#x5F0F;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;&#x53EF;&#x9009;&#x7684;&#x52A8;&#x6001;&#x8DEF;&#x5F84;&#x53C2;&#x6570;&#x3001;&#x5339;&#x914D;&#x96F6;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x3001;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#xFF0C;&#x751A;&#x81F3;&#x662F;&#x81EA;&#x5B9A;&#x4E49;&#x6B63;&#x5219;&#x5339;&#x914D;&#x3002;&#x67E5;&#x770B;&#x5B83;&#x7684; <a href="https://github.com/pillarjs/path-to-regexp#parameters" target="_blank">&#x6587;&#x6863;</a> &#x5B66;&#x4E60;&#x9AD8;&#x9636;&#x7684;&#x8DEF;&#x5F84;&#x5339;&#x914D;&#xFF0C;&#x8FD8;&#x6709; <a href="https://github.com/vuejs/vue-router/blob/next/examples/route-matching/app.js" target="_blank">&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50; </a> &#x5C55;&#x793A; <code>vue-router</code> &#x600E;&#x4E48;&#x4F7F;&#x7528;&#x8FD9;&#x7C7B;&#x5339;&#x914D;&#x3002;</p>
<h3 id="&#x5339;&#x914D;&#x4F18;&#x5148;&#x7EA7;">&#x5339;&#x914D;&#x4F18;&#x5148;&#x7EA7;</h3>
<p>&#x6709;&#x65F6;&#x5019;&#xFF0C;&#x540C;&#x4E00;&#x4E2A;&#x8DEF;&#x5F84;&#x53EF;&#x4EE5;&#x5339;&#x914D;&#x591A;&#x4E2A;&#x8DEF;&#x7531;&#xFF0C;&#x6B64;&#x65F6;&#xFF0C;&#x5339;&#x914D;&#x7684;&#x4F18;&#x5148;&#x7EA7;&#x5C31;&#x6309;&#x7167;&#x8DEF;&#x7531;&#x7684;&#x5B9A;&#x4E49;&#x987A;&#x5E8F;&#xFF1A;&#x8C01;&#x5148;&#x5B9A;&#x4E49;&#x7684;&#xFF0C;&#x8C01;&#x7684;&#x4F18;&#x5148;&#x7EA7;&#x5C31;&#x6700;&#x9AD8;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="getting-started.html" class="navigation navigation-prev " aria-label="Previous page: 开始">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="nested-routes.html" class="navigation navigation-next " aria-label="Next page: 嵌套路由">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"动态路由匹配","level":"1.3.2","depth":2,"next":{"title":"嵌套路由","level":"1.3.3","depth":2,"path":"essentials/nested-routes.md","ref":"essentials/nested-routes.md","articles":[]},"previous":{"title":"开始","level":"1.3.1","depth":2,"path":"essentials/getting-started.md","ref":"essentials/getting-started.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"essentials/dynamic-matching.md","mtime":"2017-03-04T12:16:27.892Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-06T16:00:44.406Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

